﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.Sql;
using SQLExecute;



public partial class users_register : System.Web.UI.Page
{
    public string strMessage;
    static bool bSave = false;
    User u = new User();
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            InitCombo();
    }


    public bool ValidMailAddress(string s)
    {
        Regex regEmail = new Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
        if (regEmail.IsMatch(s))
            return true;
        else return false;
    }

    protected bool EmailValid()
    {
        u.Email = txtEmail.Text;
        if (!ValidMailAddress(txtEmail.Text))
        {
            strMessage = "Địa chỉ mail không hợp lệ!";
            return false;
        }
        
        if (txtPassword.Text != txtConfirmPassword.Text)
        {
            strMessage = "Password nhập chưa khớp!";
            return false;
        }
            
            if (u.EmailExist())
            {
                strMessage = "Địa chỉ Email đăng ký đã tồn tại!";
                return false;
            }
        if (cbPhongBan.SelectedIndex==0)
        {
            
            strMessage = "Vui lòng chọn phòng ban!";

            return false;
        }

        return true;
    }
    protected void ClearText()
    {
        txtEmail.Text = "";
        txtFullname.Text = "";
        txtUserName.Text = "";
        txtPassword.Text = "";
        txtConfirmPassword.Text = "";
        cbPhongBan.SelectedIndex = 0;


    }
    protected void InitCombo()
    {
        string sql = "select * from tbl_phongban order by phongban asc";
        ds = SqlHelper.ExecuteDataset(Common.strConnect, CommandType.Text, sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            cbPhongBan.DataSource = ds;
            cbPhongBan.DataTextField = "phongban";
            cbPhongBan.DataValueField = "id";
            cbPhongBan.DataBind();
            cbPhongBan.Items.Insert(0, new ListItem("Choose One", "Choose One"));
            cbPhongBan.SelectedIndex = 0;
        }
     
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strPassword=string.Empty;
        string strEncrytPassword = string.Empty;

        if (EmailValid())
        {
            u.Password = txtPassword.Text;
            strEncrytPassword = u.Encrypt_Pass();
            u.FullName = txtFullname.Text;
            u.UserName = txtUserName.Text;
            u.PhongBanID =Convert.ToInt16(cbPhongBan.SelectedValue);
            u.Email = txtEmail.Text;
            u.GroupID = 1;
            u.Insert_New_User(strEncrytPassword);
            u.SendMail("info@vus-etsc.edu.vn", "quangtran@vus-etsc.edu.vn", "Welcome to " + txtFullname.Text + ", ", 1);
            //u.SendMail("info@vus-etsc.edu.vn", txtEmail.Text, "Welcome to " + txtFullname.Text + ", ", 1);
            ClearText();
            bSave = true;
            strMessage = "Đăng ký thành công!.";

            popupMessage.Show();

        }
        else popupMessage.Show();
            
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (bSave)
            Response.Redirect("../login.aspx");
       
    }
}
